import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

import '../entity/setting_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';

class SettingCommon extends StatefulWidget {
  const SettingCommon({Key? key}) : super(key: key);

  @override
  State<SettingCommon> createState() => _SettingCommonState();
}

class _SettingCommonState extends State<SettingCommon>  {

  SettingEntity? entity;
  final box = GetStorage();
  var _websocketAddr = "ws://localhost:10071";
  var _sip2ApiAddr = "192.168.2.100";
  var _deviceNo = "device_id=test";
  var _deviceName = "轩毅";
  var _psw = '000000';
  var _auth = '1';
  var _actionTime = 60;

  var _rotate = 2;
  double _scale1 = 1;
  double _scale2 = 1;
  double _translateX1 = 0;
  double _translateX2 = 0;
  double _translateY1 = 0;
  double _translateY2 = 0;

  @override
  initState() {
    super.initState();
    // box.remove("setting_entity");

    if(box.hasData('setting_entity')){
      entity = SettingEntity.fromJson(box.read("setting_entity"));
      if(entity!.websocketAddr != null){
        _websocketAddr = entity!.websocketAddr!;
      }
      if(entity!.sip2ApiAddr != null){
        _sip2ApiAddr = entity!.sip2ApiAddr!;
      }

      if(entity!.deviceNo != null){
        _deviceNo = entity!.deviceNo!;
      }
      if(entity!.psw != null){
        _psw = entity!.psw!;
      }
      if(entity!.deviceName != null){
        _deviceName = entity!.deviceName!;
      }
      if(entity!.auth != null){
        _auth = entity!.auth!;
      }
      if(entity!.scale1 != null){
        _scale1 = entity!.scale1!;
      }
      if(entity!.scale2 != null){
        _scale2 = entity!.scale2!;
      }
      if(entity!.translateX1 != null){
        _translateX1 = entity!.translateX1!;
      }
      if(entity!.translateX2 != null){
        _translateX2 = entity!.translateX2!;
      }
      if(entity!.translateY1 != null){
        _translateY1 = entity!.translateY1!;
      }
      if(entity!.translateY2 != null){
        _translateY2 = entity!.translateY2!;
      }
      if(entity!.rotate != null){
        _rotate = entity!.rotate!;
      }
      if(entity!.actionTime != null){
        _actionTime = entity!.actionTime!;
      }
    }else{
      entity = SettingEntity();
    }
  }

  _apiWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  webSocket地址 ",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 200.w,
          height: 20.h,
          alignment: Alignment.center,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _websocketAddr = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _websocketAddr,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _websocketAddr.length))
            )),
          ),
        ),
      ],
    );
  }
  _sip2ApiWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  服务器地址 ",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 200.w,
          height: 20.h,
          alignment: Alignment.center,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _sip2ApiAddr = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _sip2ApiAddr,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _sip2ApiAddr.length))
            )),
          ),
        ),
      ],
    );
  }


  _deviceNameWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  设备名",   style: TextStyle(fontSize: 8.sp)),
        Container(
          width: 200.w,
          height: 20.h,
          alignment: Alignment.center,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _deviceName = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _deviceName,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _deviceName.length))
            )),
          ),
        ),

        TextButton(onPressed: () {
          _saveDevice();
        }, child: Text('保存配置', style: TextStyle(fontSize: 8.sp))),
      ],
    );
  }

  _deviceNoWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  sip2参数",   style: TextStyle(fontSize: 8.sp)),
        Container(
          width: 200.w,
          height: 20.h,
          alignment: Alignment.center,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _deviceNo = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _deviceNo,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _deviceNo.length))
            )),
          ),
        ),



      ],
    );
  }

  _authWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  首位为默认方式(1=读者卡,2=身份证,3=人脸,4=账号密码)",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 40.w,
          height: 20.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            keyboardType: TextInputType.number,
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _auth = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _auth,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _auth.length))
            )),
          ),
        ),
        Text("  摄像头翻转(1-4)",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 40.w,
          height: 20.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            keyboardType: TextInputType.number,
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _rotate = int.tryParse(value)??2;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _rotate.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _rotate.toString().length))
            )),
          ),
        ),
      ],
    );
  }

  _saveDevice(){
    entity!.deviceNo = _deviceNo;
    entity!.deviceName = _deviceName;
    entity!.websocketAddr = _websocketAddr;
    entity!.sip2ApiAddr = _sip2ApiAddr;
    entity!.psw = _psw;
    entity!.auth = _auth;
    entity!.rotate = _rotate;
    entity!.actionTime = _actionTime;
    entity!.scale1 = _scale1;
    entity!.scale2 = _scale2;
    entity!.translateX1 = _translateX1;
    entity!.translateX2 = _translateX2;
    entity!.translateY1 = _translateY1;
    entity!.translateY2 = _translateY2;
    box.write('setting_entity', entity!.toJson());
    toastWarn('保存成功');
  }

  _timeWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(" 借还时间 ",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 30.w,
          height: 20.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            keyboardType: TextInputType.number,
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              if(int.tryParse(value) != null){
                _actionTime = int.tryParse(value)!;
              }
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _actionTime.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _actionTime.toString().length))
            )),
          ),
        ),
        Text("  管理员密码",   style: TextStyle(fontSize: 8.sp)),
        Container(
          width: 100.w,
          height: 20.h,
          alignment: Alignment.center,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _psw = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _psw,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _psw.length))
            )),
          ),
        ),
      ],
    );
  }

  _cameraTranslateXWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  摄像头X轴偏移1",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 50.w,
          height: 15.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _translateX1 = double.tryParse(value)??1.0;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _translateX1.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _translateX1.toString().length))
            )),
          ),
        ),

        Text("  摄像头X轴偏移2",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 50.w,
          height: 15.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _translateX2 = double.tryParse(value)??1.0;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _translateX2.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _translateX2.toString().length))
            )),
          ),
        ),
      ],
    );
  }

  _cameraTranslateYWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  摄像头Y轴偏移1",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 50.w,
          height: 20.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _translateY1 = double.tryParse(value)??1.0;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _translateY1.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _translateY1.toString().length))
            )),
          ),
        ),

        Text("  摄像头Y轴偏移2",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 50.w,
          height: 20.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _translateY2 = double.tryParse(value)??1.0;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _translateY2.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _translateY2.toString().length))
            )),
          ),
        ),
      ],
    );
  }
  _cameraScaleWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  摄像头缩放1",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 50.w,
          height: 15.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _scale1 = double.tryParse(value)??1.0;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _scale1.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _scale1.toString().length))
            )),
          ),
        ),
        Text("  摄像头缩放2",   style: TextStyle(fontSize: 8.sp),),
        Container(
          width: 50.w,
          height: 15.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 8.sp),
            onChanged: (value){
              _scale2 = double.tryParse(value)??1.0;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _scale2.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _scale2.toString().length))
            )),
          ),
        ),

      ],
    );
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: const Text("配置"),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: (){
            if(box.hasData('setting_entity')){
              settingEntity = SettingEntity.fromJson(box.read("setting_entity"));
            }
            Get.back();
          },
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [

          _deviceNameWidget(),
          _apiWidget(),
          _sip2ApiWidget(),
          _deviceNoWidget(),
          // _faceApiWidget(),
          _timeWidget(),
          _authWidget(),
          _cameraScaleWidget(),
          _cameraTranslateXWidget(),
          _cameraTranslateYWidget(),
        ],
      )
    );
  }
}

